package com.lg.baselibrary.adapter;

import android.content.Context;
import android.support.v4.util.SparseArrayCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiGang on 2017/5/27.
 */

public abstract class BaseAbsListViewAdapter<T> extends BaseAdapter{
    private List<T> mDatas;
    private Context mContext;
    private LayoutInflater mInflater;
    private SparseArrayCompat<Integer> layouts;
    public List<T> getDatas() {
        return mDatas;
    }
    public void setDatas(List<T> mDatas) {
        this.mDatas.clear();
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }
    public void addDatas(List<T> datas){
        this.mDatas.addAll(datas);
        notifyDataSetChanged();
    }
    public void addDatasFirst(T data){
        this.mDatas.add(0,data);
        notifyDataSetChanged();
    }
    public void addDatasFirst(List<T> datas){
        this.mDatas.addAll(0,datas);
        notifyDataSetChanged();
    }
    public void removeData(int index){
        this.mDatas.remove(index);
        notifyDataSetChanged();
    }
    public void removeData(T data){
        this.mDatas.remove(data);
        notifyDataSetChanged();
    }
    public Context getContext() {
        return mContext;
    }
    public LayoutInflater getInflater() {
        return mInflater;
    }

    public BaseAbsListViewAdapter(Context context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mDatas = new ArrayList<>();
        layouts = new SparseArrayCompat<>();
    }

    /**
     * 局部刷新
     * @param absListView
     * @param position 对应数据源的position，不算上头部位置
     */
    public void notifyItemChanged(AbsListView absListView,int position){
        int headerViewsCount = 0;
        if(absListView instanceof ListView){
            ListView listView = (ListView) absListView;
            headerViewsCount = listView.getHeaderViewsCount();
        }
        int firstVisiblePosition = absListView.getFirstVisiblePosition();
        int viewIndex = position + headerViewsCount - firstVisiblePosition;
        View view = absListView.getChildAt(viewIndex);
        getView(position,view,absListView);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseAbsListViewHolder viewHolder = null;
        if(convertView == null){
            int itemViewLayout = getItemViewLayout(position);
            convertView = mInflater.inflate(itemViewLayout,parent,false);
            viewHolder = new BaseAbsListViewHolder(mContext,convertView,itemViewLayout);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (BaseAbsListViewHolder) convertView.getTag();
        }
        viewHolder.setPosition(position);
        convert(viewHolder,getItem(position),position);
        return convertView;
    }

    public abstract void convert(BaseAbsListViewHolder viewHolder, T item, int position);
    @Override
    public int getItemViewType(int position) {
        int itemViewLayout = getItemViewLayout(position);
        return layouts.get(itemViewLayout);
    }
    /**必须实现该方法，返回position对应的布局文件id**/
    protected abstract int getItemViewLayout(int position);
    @Override
    public int getViewTypeCount() {
        for(int i = 0;i < mDatas.size();i++){
            int itemViewLayout = getItemViewLayout(i);
            if(layouts.get(itemViewLayout) == null){
                layouts.put(itemViewLayout,layouts.size());
            }
        }
        return layouts.size();
    }
}
